import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.settings import Settings


class Test:
    app = Settings
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1.进入设置-生物识别和密码-密码，点击关闭锁屏密码（若无"关闭锁屏密码"需要先设置一个锁屏密码）')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.biometrics_password_item)
        device.click(*self.app.lockscreen_pwd)
        device.click(*self.app.lockscreen_pwd_input_area)
        if device.keyboard_mode() != 'numpad':
            device.click(*self.keyboard.abc_123)
        for i in range(2):
            device.click(*self.keyboard.key_1)
            device.click(*self.keyboard.key_2)
            device.click(*self.keyboard.key_3)
            device.click(*self.keyboard.key_4)
            device.click(*self.keyboard.key_5)
            device.click(*self.keyboard.key_6)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('关闭锁屏密码'))
        logging.info('预期结果1.显示“是否关闭?"的提示窗口')
        device.refresh_layout()
        device.assert_type_exist('AlertDialog')
        device.assert_text_exist('取消')
        device.assert_text_exist('关闭')

        logging.info('步骤2.提示窗口点击"关闭"')
        device.click_element(device.get_element_by_text('关闭'))
        logging.info('预期结果2.进入输入锁屏密码界面')
        device.refresh_layout()
        device.refresh_layout()
        device.assert_text_exist('输入锁屏密码')
        device.assert_type_exist('TextInput')
        device.assert_text_exist('取消')
        device.assert_text_exist('下一步')

        logging.info('步骤3.点击输入框，并输入正确的锁屏密码，点击下一步')
        text_input = device.get_element_by_type('TextInput')
        device.click_element(text_input)
        device.wait(2)
        device.assert_soft_keyboard_on()
        if device.keyboard_mode() != 'numpad':
            device.click(*self.keyboard.abc_123)
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.key_3)
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.key_3)
        device.refresh_layout()
        next_step = device.get_element_by_text('下一步')
        device.click_element(next_step)
        logging.info('预期结果3.可正常唤起键盘，输入错误密码会提示“密码错误，还可尝试(<5)次”；输入正确密码点击下一步会自动返回至生物识别和密码界面')
        device.refresh_layout()
        device.assert_text_exist('密码错误，还可尝试 4 次')
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.key_3)
        device.click(*self.keyboard.key_4)
        device.click(*self.keyboard.key_5)
        device.click(*self.keyboard.key_6)
        device.click_element(next_step)
        device.refresh_layout()
        device.assert_text_exist('生物识别和密码')
        device.assert_text_exist('人脸识别')
        device.assert_text_exist('锁屏密码')

        logging.info('步骤4.设备息屏后重新进入桌面')
        device.press_power_key()
        device.wait(2)
        device.press_power_key()
        device.unlock()
        logging.info('预期结果4.设备息屏重新进入桌面不需要再输入锁屏密码')
        device.assert_current_window(self.app.window_name)
        device.refresh_layout()
        device.assert_text_exist('生物识别和密码')
        device.assert_text_exist('人脸识别')
        device.assert_text_exist('锁屏密码')
